JavaScript syntax
part 18/30 Β· 107.0 KB total
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
console.log(Boolean(this) === true);
console.log(Boolean({}) === true);
console.log(Boolean([]) === true);
// These types return false
console.log(Boolean(null) === false);
console.log(Boolean(undefined) === false); // equivalent to Boolean()
The NOT operator evaluates its operand as a Boolean and returns the
negation. Using the operator twice in a row, as a double negative,
explicitly converts an expression to a primitive of type Boolean:
console.log( !0 === Boolean(!0));
console.log(Boolean(!0) === !!1);
console.log(!!1 === Boolean(1));
console.log(!!0 === Boolean(0));
console.log(Boolean(0) === !1);
console.log(!1 === Boolean(!1));
console.log(!"" === Boolean(!""));
console.log(Boolean(!"") === !!"s");
console.log(!!"s" === Boolean("s"));
console.log(!!"" === Boolean(""));
console.log(Boolean("") === !"s");
console.log(!"s" === Boolean(!"s"));
The ternary operator can also be used for explicit conversion:
console.log([] == false); console.log([] ? true : false); // βtruthyβ,
but the comparison uses [].toString()
console.log([0] == false); console.log([0]? true : false); //
[0].toString() == "0"
console.log("0" == false); console.log("0"? true : false); // "0" β 0
... (0 == 0) ... 0 β false
console.log([1] == true); console.log([1]? true : false); //
[1].toString() == "1"
console.log("1" == true); console.log("1"? true : false); // "1" β 1 ...
(1 == 1) ... 1 β true
console.log([2] != true); console.log([2]? true : false); //
[2].toString() == "2"
console.log("2" != true); console.log("2"? true : false); // "2" β 2 ...
(2 != 1) ... 1 β true
Expressions that use features such as postβincrementation (i++) have an
anticipated side effect. JavaScript provides short-circuit evaluation of
expressions; the right operand is only executed if the left operand does
not suffice to determine the value of the expression.
console.log(a || b); // When a is true, there is no reason to evaluate
b.
console.log(a && b); // When a is false, there is no reason to evaluate
b.
console.log(c ? t : f); // When c is true, there is no reason to
evaluate f.
In early versions of JavaScript and JScript, the binary logical
operators returned a Boolean value (like most C-derived programming
languages). However, all contemporary implementations return one of
their operands instead:
console.log(a || b); // if a is true, return a, otherwise return b
console.log(a && b); // if a is false, return a, otherwise return b
Programmers who are more familiar with the behavior in C might find this
feature surprising, but it allows for a more concise expression of
patterns like null coalescing:
const s = t || "(default)"; // assigns t, or the default value, if t is
null, empty, etc.
Logical assignment
| ??= | Nullish assignment |
|---|---|
| //= | Logical Or assignment |
| &&= | Logical And assignment |
Bitwise
JavaScript supports the following binary bitwise operators:
| & | AND |
|---|---|
| / | OR |
| ^ | XOR |
| ! | NOT |
| << | shift left (zero fill at right) |
| >> | shift right (sign-propagating); copies⦠|
| >>> | shift right (zero fill at left). For po⦠|
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ